//
//  M_QuestionComment.m
//  suRead
//
//  Created by nancy  on 16/9/19.
//  Copyright © 2016年 bao. All rights reserved.
//

#import "M_QuestionComment.h"

@implementation M_QuestionComment

+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
    return @{
             @"f_id": @"id",
             @"f_wbreplyid": @"wbreplyid",
             @"f_wbdataid": @"wbdataid",
             @"f_userid": @"userid",
             @"f_username": @"username",
             @"f_img": @"img",
             @"f_ip":@"ip",
             @"f_inputtime": @"inputtime",
             @"f_status":@"status",
             @"f_content": @"content",
             @"f_support":@"support",
             @"f_reply":@"reply",
             @"f_commnums":@"commnums"
             };

}

+ (NSValueTransformer *)f_contentJSONTransformer {
    
    return [MTLValueTransformer transformerUsingForwardBlock:^(NSString *value_, BOOL *success, NSError **error){
        
        BOOL iOS8OrLater = kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_8_0;
        
        if (iOS8OrLater) {
            //去掉html标签
            if ([value_ containsString:@"</span>"]) {
                value_ = [value_ stringByReplacingOccurrencesOfString:@"<br>" withString:@""];
                return value_;
                
            }
            else {
                return value_;
            }
        }
        else
        {
            NSRange range = [value_ rangeOfString:@"</span>"];
            if (range.location == NSNotFound) {
                return value_;
            }
            else {
                value_ = [value_ stringByReplacingOccurrencesOfString:@"<br>" withString:@""];
                return value_;
            }
        }
    }];
    
}

+ (NSValueTransformer *)f_inputtimeJSONTransformer {
    
    return [MTLValueTransformer transformerUsingForwardBlock:^(NSNumber *value_, BOOL *success, NSError **error){
        
        if (!value_) {
            return @" ";
        }
        
        double deltaSeconds = fabs([[NSDate date] timeIntervalSince1970] - value_.doubleValue); // 获取总共秒数
        double deltaMinutes = deltaSeconds / 60.0f;
        
        NSString *dateContent;
        if (deltaSeconds < 5) {
            dateContent = @"刚刚";
        } else if (deltaSeconds < 120) {
            dateContent = @"1分钟前";
        } else if (deltaMinutes < 60) {
            dateContent = [NSString stringWithFormat:@"%i分钟前", (int)deltaMinutes];
        } else if (deltaMinutes < 120) {
            dateContent = @"1小时前";
        } else if (deltaMinutes < (24 * 60)) {
            dateContent = [NSString stringWithFormat:@"%i小时前", (int)floor(deltaMinutes / 60)];
        } else if (deltaMinutes < (24 * 60 * 2)) {
            dateContent = @"昨天";
        } else if (deltaMinutes < (24 * 60 * 7)) {
            dateContent = [NSString stringWithFormat:@"%d天前", (int)floor(deltaMinutes/(60 * 24))];
        } else if (deltaMinutes < (24 * 60 * 14)) {
            dateContent = @"上周";
        } else if (deltaMinutes < (24 * 60 * 31)) {
            dateContent = [NSString stringWithFormat:@"%d周前", (int)floor(deltaMinutes/(60 * 24 * 7))];
        } else if (deltaMinutes < (24 * 60 * 61)) {
            dateContent = @"上个月";
        } else if (deltaMinutes < (24 * 60 * 365.25)) {
            dateContent = [NSString stringWithFormat:@"%d月前", (int)floor(deltaMinutes/(60 * 24 * 30))];
        } else if (deltaMinutes < (24 * 60 * 731)) {
            dateContent = @"去年";
        } else {
            dateContent = [NSString stringWithFormat:@"%i年前", (int)floor(deltaMinutes/(60 * 24 * 731))];
        }
        
        return dateContent;
        
    } reverseBlock:^(NSString *id_, BOOL *success, NSError **error) {
        return id_;
    }];
    
}

@end